package com.ck.ips.service.impl.file;

import com.ck.common.model.FileGroup;
import com.ck.common.model.OperationResult;
import com.ck.ips.dao.file.FileGroupMapper;
import com.ck.ips.dao.file.FileMapper;
import com.ck.ips.model.vo.FileGroupVO;
import com.ck.ips.service.file.FileGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @description: fileGroupService实现类
 * @author: zhanglei
 * @date: 2019/4/30 14:49
 */
@Service("fileGroupService")
public class FileGroupServiceImpl implements FileGroupService {

    @Autowired
    private FileGroupMapper fileGroupMapper;
    @Autowired
    private FileMapper fileMapper;

    @Override
    public int getFileGroupTotal() {
        int total = fileGroupMapper.getFileGroupTotal();
        return total;
    }

    @Override
    public boolean checkGroupNameExist(String groupName) {
        int count = fileGroupMapper.checkGroupNameExist(groupName);
        if(count>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean outSelfGroupNameExist(String fileGroupName, Long fileGroupId) {
        //获取原来的文件分组信息
        FileGroup oldFileGroup = fileGroupMapper.getFileGroupById(fileGroupId);
        String oldFileGroupName = oldFileGroup.getFileGroupName();
        int count = fileGroupMapper.checkGroupNameExist(fileGroupName);
        if(count>0){
            if(oldFileGroupName.equals(fileGroupName) && count==1){
                return false;
            }
            return true;
        }
        return false;
    }

    @Override
    public OperationResult<Object> addFileGroup(String groupName, Long loginUserId, Long nowTime) {
        //新增分组
        fileGroupMapper.addFileGroup(groupName,loginUserId,nowTime);
        //增加日志
        String msg = "增加素材分组["+groupName+"]";
        return OperationResult.newSuccessTipsOperationResult(msg);
    }

    @Override
    public OperationResult<Object> editFileGroup(Long fileGroupId, String fileGroupName, Long loginUserId, Long nowTime) {
        FileGroup oldFileGroup = fileGroupMapper.getFileGroupById(fileGroupId);
        //编辑分组
        if(oldFileGroup!=null){
            fileGroupMapper.editFileGroup(fileGroupId,fileGroupName,loginUserId,nowTime);
            //保存日志
            String msg = "修改素材分组:"+oldFileGroup.getFileGroupName()+"名称为"+"["+fileGroupName+"]";
            return OperationResult.newSuccessTipsOperationResult(msg);
        }else {
            return OperationResult.newValidateFailOperationResult("分组不存在");
        }
    }

    @Override
    public OperationResult<Object> deletebyFileGroupIds(Long loginUserId, String fileGroupIds) {
        //添加日志
        //获取需要删除的文件分组id
        List<String> fileGroupIdList = Arrays.asList(fileGroupIds.split(","));
        //将原来选择该分组的素材分组修改为默认分组
        fileGroupMapper.updateFileDefaultGroup(fileGroupIdList);
        //记录日志
        String deleteGroupNames = fileGroupMapper.getFileGroupNames(fileGroupIdList);
        //删除分组
        fileGroupMapper.deletebyFileGroupIds(fileGroupIdList);
        //返回数据存日志
        String msg = "删除素材分组["+deleteGroupNames+"]";
        return OperationResult.newSuccessTipsOperationResult(msg);
    }

    @Override
    public List<FileGroupVO> getNotApiFileGroup() {
        List<FileGroupVO> fileGroupList = fileGroupMapper.getNotApiFileGroup();
        return fileGroupList;
    }

    @Override
    public void addFileGroupVOTotal(List<FileGroupVO> fileGroupVOList, String fileType) {
        for(FileGroupVO fileGroup:fileGroupVOList){
            int total = fileMapper.getTotalByFileTypeGroup(fileType,fileGroup.getFileGroupId());
            fileGroup.setFiletotal(total);
        }
    }

    @Override
    public Long getDefaultGroupId() {
        return fileGroupMapper.getDefaultGroupId();
    }
}
